package cn.ulc.socket.admin.service.common.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.AliyunOssFileUtil;
import cn.ulc.socket.admin.service.common.CommonServ;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Service
public class CommonServImpl implements CommonServ {

    @Override
    public String uploadFileBase64(String data) {
        Base64 base64 = new Base64();
        String realPath = "";
        try{
            byte[] k = base64.decode(data.substring("data:image/jpeg;base64,"
                    .length()));
            InputStream is = new ByteArrayInputStream(k);
            String fileName = IdUtil.randomUUID() + ".jpg";
            realPath = AliyunOssFileUtil.uploadPublicFile(is,fileName,is.available());
            if(StrUtil.isBlank(realPath)) new PlatformException("音频上传失败 ~ ~");
        }catch (Exception e) {
            e.printStackTrace();
            throw new PlatformException("上传海报异常!");
        }
        return realPath;
    }
}
